package insigne.kernel.sgbdinterface.basics;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;


public class Faluchard extends DBOperator{

	/* ############################### Fields ############################### */
	private int ID;

	private String NomFaluchard;

	private String PrenomFaluchard;

	private String SurnomFaluchard;

	private Listeinsignes IDListe;

	private String adherent;
	
	private static ArrayList<Faluchard> faluchards;

	/* ############################### Getters ############################## */
	public int getID(){
		return ID;
	}

	public String getNomFaluchard(){
		return NomFaluchard;
	}

	public String getPrenomFaluchard(){
		return PrenomFaluchard;
	}

	public String getSurnomFaluchard(){
		return SurnomFaluchard;
	}

	public Listeinsignes getIDListe(){
		return IDListe;
	}

	public String getadherent(){
		return adherent;
	}

	/* ############################### Setters ############################## */
	public void setID(int newValue){
		this.ID = newValue;
	}

	public void setNomFaluchard(String newValue) throws SQLException{
		openConnection();
		this.NomFaluchard = newValue;
		executeUpdate("UPDATE faluchard SET  `NomFaluchard` =  '"+NomFaluchard+"' WHERE  ID='"+ID+"'");
		con.close();
	}

	public void setPrenomFaluchard(String newValue) throws SQLException{
		openConnection();
		this.PrenomFaluchard = newValue;
		executeUpdate("UPDATE faluchard SET  `PrenomFaluchard` =  '"+PrenomFaluchard+"' WHERE  ID='"+ID+"'");
		con.close();
	}

	public void setSurnomFaluchard(String newValue) throws SQLException{
		openConnection();
		this.SurnomFaluchard = newValue;
		executeUpdate("UPDATE faluchard SET  `SurnomFaluchard` =  '"+SurnomFaluchard+"' WHERE  ID='"+ID+"'");
		con.close();
	}

	public void setIDListe(Listeinsignes newValue) throws SQLException{
		openConnection();
		this.IDListe = newValue;
		executeUpdate("UPDATE faluchard SET  `IDListe` =  '"+IDListe.getIDListe()+"' WHERE  ID='"+ID+"'");
		con.close();
	}

	public void setadherent(String newValue) throws SQLException{
		openConnection();
		this.adherent = newValue;
		executeUpdate("UPDATE faluchard SET  `adherent` =  '"+adherent+"' WHERE  ID='"+ID+"'");
		con.close();
	}

	/* ########################### CLASS CONSTRUCTORS ####################### */
	public Faluchard() {
		// TODO Auto-generated constructor stub
	}
	
	public Faluchard(String NomFaluchard,String PrenomFaluchard,String SurnomFaluchard,String adherent) throws SQLException {
		openConnection();
		this.NomFaluchard = NomFaluchard;
		this.PrenomFaluchard = PrenomFaluchard;
		this.SurnomFaluchard = SurnomFaluchard;
		this.IDListe = new Listeinsignes(new Date(System.currentTimeMillis()),999);
		this.adherent = adherent;
		this.ID = executeUpdate("INSERT INTO faluchard VALUES('DEFAULT','"+NomFaluchard+"','"+PrenomFaluchard+"','"+SurnomFaluchard+"','"+IDListe.getIDListe()+"','"+adherent+"')");
		con.close();
		this.IDListe.setIDFaluchard(this.getID());
		faluchards.add(this);
	}

	public Faluchard(int ID) throws SQLException {
		openConnection();
		ResultSet rs = executeSelect("SELECT * FROM faluchard WHERE ID='"+ID+"'");
		rs.next();
		this.ID = rs.getInt("ID");
		this.NomFaluchard = rs.getString("NomFaluchard");
		this.PrenomFaluchard = rs.getString("PrenomFaluchard");
		this.SurnomFaluchard = rs.getString("SurnomFaluchard");
		this.IDListe = new Listeinsignes(rs.getInt("IDListe"));
		this.adherent = rs.getString("adherent");
		con.close();
	}
	


	/* retourne tous les insignes de la bases sans conditions */
	public ArrayList<Faluchard> getAllFaluchards() throws SQLException {
		if (faluchards == null) {
			openConnection();
			faluchards = new ArrayList<Faluchard>();
			ResultSet rs = executeSelect("SELECT * FROM faluchard");
			while (rs.next()) {
				Faluchard fal = new Faluchard();
				fal.ID = rs.getInt("ID");
				fal.NomFaluchard = rs.getString("NomFaluchard");
				fal.PrenomFaluchard = rs.getString("PrenomFaluchard");
				fal.SurnomFaluchard = rs.getString("SurnomFaluchard");
				fal.adherent = rs.getString("adherent");
				fal.IDListe = new Listeinsignes(rs.getInt("IDListe"));
				faluchards.add(fal);
			}
			con.close();
		}
		return faluchards;
	}
	
	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.SurnomFaluchard;
	}
}